package com.carcomponent;

import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baseres.BaseActivity;
import com.baseres.router.ArouterConsts;
import com.opensource.svgaplayer.SVGACallback;
import com.opensource.svgaplayer.SVGADrawable;
import com.opensource.svgaplayer.SVGAImageView;
import com.opensource.svgaplayer.SVGAParser;
import com.opensource.svgaplayer.SVGAVideoEntity;

@Route(path = ArouterConsts.CAR_LIST)
public class CarListActivity extends BaseActivity implements View.OnClickListener {

    private SVGAImageView mGiftAnimSIV;
    private SVGAParser mSVGAParse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.car_activity_car);
        mGiftAnimSIV = findViewById(R.id.svgaimageview);
        Button car_button1 = findViewById(R.id.car_button1);
        Button car_button2 = findViewById(R.id.car_button2);
        Button car_button3 = findViewById(R.id.car_button3);
        Button car_button4 = findViewById(R.id.car_button4);
        car_button1.setOnClickListener(this);
        car_button2.setOnClickListener(this);
        car_button3.setOnClickListener(this);
        car_button4.setOnClickListener(this);

    }

    /**
     * 播放礼物动画
     */
    private void playGiftAnimation(String name) {

        mSVGAParse = SVGAParser.Companion.shareParser();
        mSVGAParse.init(this);
        mSVGAParse.decodeFromAssets(name, new SVGAParser.ParseCompletion() {

            @Override
            public void onError() {

            }

            @Override
            public void onComplete(SVGAVideoEntity mSVGAVideoEntity) {
                Log.d("luoli","onComplete");
                mGiftAnimSIV.setVisibility(View.VISIBLE);
                SVGADrawable drawable = new SVGADrawable(mSVGAVideoEntity);
                mGiftAnimSIV.setImageDrawable(drawable);
                mGiftAnimSIV.startAnimation();
            }
        });
        mGiftAnimSIV.setCallback(new SVGACallback() {
            @Override
            public void onPause() {
                Log.d("luoli","onPause");
            }

            @Override
            public void onFinished() {
                Log.d("luoli","onFinished");
                mGiftAnimSIV.setVisibility(View.GONE);
            }

            @Override
            public void onRepeat() {
                Log.d("luoli","onRepeat");
            }

            @Override
            public void onStep(int frame, double percentage) {
                Log.d("luoli","onStep   "+"frame:"+frame+"   percentage:"+percentage);
            }
        });
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.car_button1) {
            playGiftAnimation("ga32.svga");
        }else if (id == R.id.car_button2) {
            playGiftAnimation("ga31.svga");
        }else if (id == R.id.car_button3) {
            playGiftAnimation("ga27.svga");
        }else if (id == R.id.car_button4) {
            playGiftAnimation("ga28.svga");
        }
    }
}
